class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def largestValues(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        out = []
        stack = [root]
        while stack:
            now_max = stack[0].val
            next_stack = []
            for node in stack:
                now_max = max(now_max, node.val)
                if node.left:
                    next_stack.append(node.left)
                if node.right:
                    next_stack.append(node.right)
            stack = next_stack
            out.append(now_max)
        return out

